// 扫码核销页面

const app = getApp();

Page({
  data: {
    verificationStatus: 'loading', // loading: 加载中, success: 获取信息成功, fail: 获取信息失败, confirmed: 已确认核销
    orderInfo: null,
    errorMessage: '',
    activity_id: '',
    sign_id: '',
    loadingText: '正在获取核销信息...'
  },

  onLoad: function(options) {
    // 从扫码参数中获取activity_id和sign_id
    if (options.activity_id && options.sign_id) {
      this.setData({
        activity_id: options.activity_id,
        sign_id: options.sign_id
      });
      // 调用获取核销信息接口
      this.getWriteOffInfo(options.activity_id, options.sign_id);
    } else if (options.scene) {
      // 处理小程序码场景值
      const scene = decodeURIComponent(options.scene);
      const params = this.parseSceneParams(scene);
      if (params.activity_id && params.sign_id) {
        this.setData({
          activity_id: params.activity_id,
          sign_id: params.sign_id
        });
        this.getWriteOffInfo(params.activity_id, params.sign_id);
      } else {
        this.handleVerificationError('参数错误，请扫描正确的二维码');
      }
    } else {
      this.handleVerificationError('未获取到核销信息，请扫描正确的二维码');
    }
  },

  // 解析场景参数
  parseSceneParams: function(scene) {
    const params = {};
    const pairs = scene.split('&');
    pairs.forEach(pair => {
      const [key, value] = pair.split('=');
      if (key && value) {
        params[key] = value;
      }
    });
    return params;
  },

  // 调用获取核销信息接口
  getWriteOffInfo: function(activity_id, sign_id) {
    const that = this;
    
    app._getNetWork({
      url: app.getNetAddresss('plugin.commerce.api.commerce-activity.get-write-off'),
      data: {
        activity_id: activity_id,
        sign_id: sign_id
      },
      success: function(res) {
        var response = res.data;
        if (response.result === 1) {
          // 获取信息成功
          that.setData({
            verificationStatus: 'success',
            orderInfo: response.data
          });
        } else {
          // 获取信息失败
          that.handleVerificationError(response.msg || '获取核销信息失败，请稍后重试');
        }
      },
      fail: function() {
        console.error('获取核销信息接口调用失败');
        that.handleVerificationError('网络异常，请检查网络后重试');
      },
      complete: function() {
        // 可以在这里添加加载完成后的处理
      },
      state: true
    });
  },

  // 确认核销
  confirmWriteOff: function() {
    const that = this;
    
    wx.showLoading({
      title: '确认核销中...',
    });
    
    app._getNetWork({
      url: app.getNetAddresss('plugin.commerce.api.commerce-activity.confirm-write-off'),
      data: {
        activity_id: that.data.activity_id,
        sign_id: that.data.sign_id
      },
      success: function(res) {
        var response = res.data;
        if (response.result === 1) {
          // 确认核销成功
          that.setData({
            verificationStatus: 'confirmed'
          });
          wx.showToast({
            title: '核销成功',
            icon: 'success'
          });
        } else {
          // 确认核销失败
          wx.showToast({
            title: response.msg || '核销失败，请稍后重试',
            icon: 'none'
          });
        }
      },
      fail: function() {
        console.error('确认核销接口调用失败');
        wx.showToast({
          title: '网络异常，请检查网络后重试',
          icon: 'none'
        });
      },
      complete: function() {
        wx.hideLoading();
      },
      state: true
    });
  },

  // 处理核销失败
  handleVerificationError: function(message) {
    this.setData({
      verificationStatus: 'fail',
      errorMessage: message
    });
  },

  // 重新核销
  reVerify: function() {
    this.setData({
      verificationStatus: 'loading',
      errorMessage: ''
    });
    this.getWriteOffInfo(this.data.activity_id, this.data.sign_id);
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  },

  // 查看订单详情
  viewOrderDetail: function() {
    if (this.data.orderInfo && this.data.orderInfo.id) {
      wx.navigateTo({
        url: `/packageX/order_detail/order_detail?id=${this.data.orderInfo.id}`
      });
    }
  }
});